
/**
 * Copyright (c) 2011,赛维航电科技有限公司软件开发部
 * All rights reserved.
 *
 * @file EFBGlobalData.java
 * @brief ...
 * @details ActivityStackControlUtil        ... \n
 * @author 吴盈
 * @version 1.0
 * @history 修订信息 \n
 * 吴盈  | 2011-12-10 | V1.0  |设计类：AppGlobalData \n
 */

package com.savi.myapplication;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

//import com.savi.gpsemap.GPS;
//import com.savi.savikneeboard.PinchImageView.ImageSource;
//import com.savi.savikneeboard.PinchImageView.SaviImageSource;
//import com.savi.savikneeboard.RecordInfo.SvRecordInfo;
//import com.savi.savikneeboard.flytask.FlyTask;

/**
 * @author wuying
 * @version 1.0
 * @brief 全局数据类
 */
public class EFBGlobalData extends Application {
    public final static int FLY_OVER_DISTANCWE = 3;//判断是否到达航路点距离阈值，即距离航路点该距离时任务已到达该航路点，单位（公里）。
    public final static int WARN_DISTANCE = 30;//任务提示距离阈值，即距离任务点该距离时开始弹出窗口提示，单位（公里）。

    //public FlyTask flyTask;//飞行任务

    public int m_mapType;                            //地图类型（1：矢量，4：高程，99：卫星）
    final public static int mtInvalid = 0;            ///< 无效
    final public static int mtImg = 1;              ///< Img矢量地图
    final public static int mtSatellite = 3;        ///< 卫星地图
    final public static int mtSatelliteImg = 4;    ///< 卫星地图矢量地图叠加
    final public static int mtDEM = 5;                ///< 高程地图
    final public static int mtDEMImg = 6;            ///< 高程地图叠加矢量地图
    final public static int mtMbTiles = 99;        ///< 高德混合地图 测试用
    public int m_HeadingType;                        //地图方向（0：真北朝上，1：机头朝上）
    final public static int htZhenbei = 0;         ///< 真北朝上
    final public static int htJitou = 1;           ///< 机头朝上
    //	public int m_ToolType;							//工具类型（0：无效，1：距离测量，2：面积测量）
    final public static int ttInvalid = 0;         ///< 无效
    final public static int ttDistance = 1;        ///< 距离测量
    final public static int ttArea = 2;                ///< 面积测量
    //	public int m_MapViewType;						//地图页面类型
    final public static int vtRealMap = 0;         ///< 真实导航页面
    final public static int vtSimuMap = 1;         ///< 模拟导航页面

    public List<String> g_lsImageList;//图片列表
    public boolean bIsDay = true;  //是否白天

    //GPS信息对象
    public LocationManager m_Lm;

    //数据库对象

    //电子卡片页面类型
//	public int m_ElecCard_Activity_Type;//0：无效，1：PDF，2：图片

    public static String gStoragePath = "";
    public static String gEFBDataStoragePath = "";

    public static String gMFBDataStoragePath = "";
    public static String EFB_DATA_PATH = "/EFBdata";

    public static String MFB_DATA_PATH = "/MFBdata";
    //飞行卡片模板
    public static String MFB_DATA_FLY_CARD_PATH = "/FlyCard";
    public static String EFB_DATA_DOCMENTS = "/docments";
    public static String EFB_DATA_CUSTOM_DOCMENTS = "/custom_docments";
    public static String EFB_DATA_WORK = "/work_docments";
    //EFB资料功能
    public static String EFB_PILOT_FILE = "/MFB飞行员资料";

    public static String EFB_DATA_WORK_NOTICE = "/Notice";
    //计划机场文件夹
    public static String EFB_DATA_WORK_PLAN_AIRPORT = "/PlanAirport";
    //计划转场文件夹
    public static String EFB_DATA_WORK_PLAN_ZC = "/PlanZC";
    //计划任务文件夹
    public static String EFB_DATA_WORK_PLAN_TASK = "/PlanTask";
    //航线资料文件夹
    public static String EFB_DATA_WORK_NAVIGATION = "/Navigation";
    //Apk更新存放的路径
    public static String EFB_DATA_WORK_UPDATA_APK = "/updateApk";
    public static String EFB_DATA_DATABASE = "/database";
    public static String EFB_DATA_BACKUPDATABASE = "/backup";

    public static String MFB_DATA_DATABASE = "/database";
    public static String MFB_DATA_BACKUPDATABASE = "/backup";
    public static String EFB_TEMPFILES = "/tempfiles";
    public static String EFB_DATA_TASKS = "/tasks";
    public static String EFB_DATA_MAP = "/map";


    public static String UPDATE_LOG_FILE_PATH = "updateLog.csv";  //更新日志的文件路径

    public static String CSV_FILE_PATH = "/";///< 要读取的jyCSV文件路径
    public static String ARMY_FILE_PATH = "/FlyData";///< 要读取的jyCSV文件名称
    public static String CSV_FILE_NAME = "/airport.csv";///< 要读取的jyCSV文件名称
    public static String AIS_AISDB_NAME = "/ais.db";///< 数据库名称
    public static String EFB_NAVIDB_NAME = "/navid.db";///< 数据库名称
//    public static String EFB_NAVIDB_NAME_Global = "/navidGlobal.db";///< 数据库名称
    public static String EFB_NAVIDB_NAME_Global = "/GlobalDB.db";///< 数据库名称  GlobalDB.db
    public static String EFB_EFBDB_NAME = "/EFBdatabase.db";

    public static String MFB_EFBDB_NAME = "/MFBdatabase.db";
    public static String EFB_EFBDB_NAME_GLOBAL = "/EFBdatabaseGlobal.db";
    public static String EFB_EFBDB_NAME_GLOBAL_Doc = "";
    public static String EFB_DATA_NAIP = "/NAIP";
    public static String EFB_DATA_NAIP_GL = "/NAIPGLOBAL/DOC";

    public static String EFB_DATA_NAIP_CURRENT = EFB_DATA_NAIP + "/CURRENT";
    public static String EFB_DATA_NAIP_UPDATE = "/updateNAIP";

    public static String AIS_RUSSIA_NAME = "/russia_ais.db";

    public static String UPDATE_NAIP_ZIP = "/NAIP.zip";
    public static String SPECIAL_CONDITION_PDF = "/[QRH]快速检查单.pdf";     //快速检查单  -miao   名字修改[ORH]，，，

    public static String CAMERA_PATH = EFB_DATA_PATH + "/photo";

//
//    public static PlaneInfoDao planeInfoDao;
//    public static FlyPropertyDao flyPropertyDao;
//    public static SubjectInfoDao subjectInfoDao;
//    public static TaskPropertyInfoDao taskPropertyInfoDao;
//    public static OutlineInfoDao outlineInfoDao;
//    public static TrainInfoDao trainInfoDao;
//    public static PersonInfoDao personInfoDao;
//    //机场风险等级
//    public static RiskLevelInfoDao riskLevelInfoDao;



    //是否反色显示（夜视模式）
    public int isInvert = 0;

    //是否锁定屏幕 0锁定  1竖屏  2横屏
    public int lockScreen = 0;

    public boolean bKeyboardCover = false;

    //用户是否已登录
    public boolean isLogin = false;
    //用户是否为特情处置
    public boolean isSecretDisposal = false;
    //航线中机场点的ID
    public List<Integer> gCurAirportIdList = null;
    public List<Integer> gCurArmyAirportIdList = null;

    public boolean isShowCustomPoint = true;
    public boolean isShowDistanceCircle = false;     //显示中心圈  miao
    public boolean isAisContentDraw = true;

    public boolean bAll = false;
    public boolean bVOR = true;
    public boolean bDEM = true;
    public boolean bPpoint = true;
    public boolean bAirport = true;
    public boolean bAirportArmy = true;    //jy机场是或否勾选
    public boolean bSegment = true;
    public boolean bSpecialSegment = true;
    public boolean bLongLaGrid = true;
    public boolean bBoundRegion = true;
    public boolean bACCRegion = true;
    public boolean bFIRRegion = true;
    public boolean bFollowMove = false;
    public boolean bMapFollowGPS = true;         //!< 地图随动GPS位置
    public boolean bMsearch=false;
    public boolean bCTA_SECTORRegion=true;		 //!< 管制扇区
    public boolean bAPPRegion=true;				 //!< 进近管制区
    public boolean bAPP_SECTORRegion=true;		 //!< 进近管制扇区
    public boolean bShowCustomAirspace=true;		 //!< 是否显示自定义空域
    public boolean isPassword=true;		        //!< 数据库是否有密码
    //是否显示地名
    public boolean bPlaceName=true;
    //是否显示边界
    public boolean bBorder=true;
    //是否显示道路
    public boolean bRoad=true;
    //是否显示水体
    public boolean bWater=true;

    //选中的是机场
    public boolean bShowAirport=false;
    //选中的是空域
    public boolean bShowAirspace=false;
    //选中的是航路点
    public boolean bShowAirwayPt=false;
    //fir //新图层存放事发后绘制变量
    public boolean bBorderFir=false;
    //选中的是vor、ndb
    public boolean bShowVorNdb=false;
    //选中的是航段
    public boolean bShowAirway=false;
    //选中的是路网
    public boolean bShowHighway=false;

    //选中的是卫星地图
    public boolean bSatelliteMap=false;
    //选中的是者矢量地图
    public boolean bVectorMap=false;
    //选中的是者矢量地图
    public boolean bElevationMap=false;
    //空间盒显示控制开关
    public boolean bShowSpaceBox=false;


    public float MAX_SCALE=500.0f;		 //!< 是否显示自定义空域
    public String currentOpenFileName="";		 //!< 记录自定义资料当前打开的文件名
//    public SettingPopupWindow settingPopupWindow;

    public boolean isCustomFileOpen=false;             //自定义文件是否打开
    public boolean isPreview=false;             //是否选择预览
    public String previewJson="";               //保存后准备预览的json文件

//    public SysConfig sysConfig=new SysConfig();  //保存中心圈状态到配置文件

    public int iDefaultAirwayColor = 0x40035202;         // 0xAA035202    航线默认颜色修改   miao   2022-06-17

    public boolean isStartAutoAirway = false;    //开启自动航线

    public SharedPreferences configSp;

    public boolean isShowDBExistDialog = true;

    public static int widthSet;   //屏幕的宽度

    public static int heightSet;  //屏幕的高度

    public static int widthPad;   //屏幕的宽度

    public static int heightPad;  //屏幕的高度

    public static String m_strEFBdbPass = "saviefb";  //数据库加密密码

    public static String m_strEFBdbOutPath = gEFBDataStoragePath + "/backup/EFBdatabase.db"; //数据库恢复路径

    public int selectNow=-1;    //修改航线名称 选中当前航线的id

    public int selectAfter=-1;  //修改航线名称选择下一条时的id

    public boolean alreadyisSpeCondition = false;   //是否为特勤 解决点击特勤后 页面显示问题  miao  20201207

    public boolean photoState = false;     //照片视图的状态  20201210  miao   解决拍照后点击特情显示问题

//    public LoadingDialog loading=null;

    public static int dragIndex=-1;       //拖拽位置索引
    //选择的进场程序最后一个点名称
    public String selectStarLastPointName ="";
    public String m_selNaviTransStar="";;
    //是否全球数据
    public static boolean isGlobalData = true;
    //点击航线列表进入FPL
    public boolean isClickAirwayList = false;
    //是否在FPL中选择公司航路
    public boolean isFPLSelectCompanyRoute = false;
    //是否在FPL中选择添加航路
    public boolean isFPLSelectAddPT = false;
    //选择全球航图标志，机场PDF判断使用
    public boolean selectedGLChart = false;
    //是否从FPL中规划公司航路
    public boolean fromFPLCompany = false;


    //是否显示任务菜单
    public boolean isShowTabTask = false ;
    //是否显示任务卡片菜单
    public boolean isShowTabCard = false ;

    public static float land_widthPixels = 1920.0f;
    public static float portrait_widthPixels = 1200.0f;
    /**
     * @brief 构造函数
     */
    public EFBGlobalData() {
        //flyTask = null;
        m_mapType = 1;//矢量
        m_HeadingType = 1;//机头朝上
        g_lsImageList = null;
//        m_Gps = null;
        m_Lm = null;
//        m_AISdb = null;
    }

    /**
     *
     */
    @Override
    public void onTerminate() {
        Log.d("Application", "onTerminate()");
//        SvMapManageAppJni.term();
//        FileEncryptor.clearDecryptedFile(gStoragePath + EFB_DATA_PATH + EFB_TEMPFILES);
        super.onTerminate();
    }


    //	private int mLock = 0;
    @Override
    public void onCreate() {

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                Log.d("Application", "onTerminate()");
            }

            @Override
            public void onActivityStarted(Activity activity) {
                Log.d("Application", "onTerminate()");
            }

            @Override
            public void onActivityResumed(Activity activity) {

                Log.d("Application", "onTerminate()");
            }

            @Override
            public void onActivityPaused(Activity activity) {

                Log.d("Application", "onTerminate()");
            }

            @Override
            public void onActivityStopped(Activity activity) {

                Log.d("Application", "onTerminate()");
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

                Log.d("Application", "onTerminate()");
            }

            @Override
            public void onActivityDestroyed(Activity activity) {

                Log.d("Application", "onTerminate()");
            }
        });
        super.onCreate();

        configSp = this.getSharedPreferences("config", Context.MODE_PRIVATE);
        bKeyboardCover = configSp.getBoolean("keyboard_cover", false);

    }

    /**
     * 设置屏幕的像素（适配不同大小的手机）
     */
    public static void setScreenAdapter(Context context) {

        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
//            widthSet = displayMetrics.widthPixels * 5 / 10;
//            heightSet = displayMetrics.heightPixels * 6 / 10;
//            widthPad = displayMetrics.widthPixels;
//            heightPad = displayMetrics.heightPixels;
            widthSet = displayMetrics.heightPixels * 6 / 10;         //  1132
            heightSet = (displayMetrics.widthPixels-68) * 5 / 10;         //  1920
            widthPad = displayMetrics.heightPixels+70;
            heightPad = displayMetrics.widthPixels;
        } else if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            widthSet = displayMetrics.widthPixels * 6 / 10;
            heightSet = displayMetrics.heightPixels * 5 / 10;
            widthPad = displayMetrics.widthPixels;             //1200
            heightPad = displayMetrics.heightPixels;           //1852
        }
        //int sw =context.getResources().getConfiguration().smallestScreenWidthDp;
        //Log.e("setScreenAdapter "+ displayMetrics.densityDpi +"--"+displayMetrics.density, "setScreenAdapter:widthSet "+displayMetrics.widthPixels +"-- "+displayMetrics.heightPixels+"--- sw"+ sw);
    }



}